﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.ChuongTrinhDaoTaoService;
using System.ServiceModel;
namespace eTMS.Controllers
{
    public class ChuongTrinhDaoTaoControllerBase : CommonController
    {
        public ChuongTrinhDaoTaoControllerBase()
            : base()
        {
            this.dao = new ChuongTrinhDaoTaoServiceClient();
        }
        ~ChuongTrinhDaoTaoControllerBase()
        {
            if (dao.State != CommunicationState.Closed)
                if (dao.State == System.ServiceModel.CommunicationState.Opened) try { dao.Close(); }
                    catch { }
        }
        private DataTable _Table;
        private ChuongTrinhDaoTaoEntity oChuongTrinhDaoTao;
        public ChuongTrinhDaoTaoEntity ChuongTrinhDaoTaoObject
        {
            get
            {
                return oChuongTrinhDaoTao;
            }
            set
            {
                oChuongTrinhDaoTao = value;
            }
        }
        protected ChuongTrinhDaoTaoServiceClient dao;
        public DataTable Table
        {
            get
            {
                this.LoadAllDSChuongTrinhDaoTao(false);
                return _Table;
            }
        }
        public void LoadAllDSChuongTrinhDaoTao(bool clearCache)
        {
            bool bForce = clearCache;
            if (_Table == null)
            {
                bForce = true;
            }
            if (!bForce) return;
            DataSet ds = dao.SelectAllDS();
            if (ds.Tables.Count > 0)
            {
                _Table = ds.Tables[0];
                this.AddIndexToTable(_Table);
            }
        }
        public void AddIndexToTable()
        {
            this.AddIndexToTable(_Table);
        }
        public void ResetTableIndex()
        {
            this.ResetTableIndex(_Table);
        }
        public bool AddNew()
        {
            if (oChuongTrinhDaoTao == null) return false;
            bool result = true;
            try
            {
                oChuongTrinhDaoTao = dao.SaveRet(oChuongTrinhDaoTao);
            }
            catch
            {
                result = false;
            }
            if (result)
            {
                _ListChuongTrinhDaoTao.Add(oChuongTrinhDaoTao);
            }
            return result;
        }
        public bool Update()
        {
            if (oChuongTrinhDaoTao == null) return false;
            return dao.Update(oChuongTrinhDaoTao);
        }
        public bool Delete()
        {
            if (oChuongTrinhDaoTao == null) return false;
            bool retvalue = dao.Delete(oChuongTrinhDaoTao);
            if (retvalue)
            {
                _ListChuongTrinhDaoTao.Remove(oChuongTrinhDaoTao);
            }
            return retvalue;
        }
        public bool DeleteByID(Int32 ChuongTrinhDaoTao_ID)
        {
            bool retvalue = dao.DeleteByID(ChuongTrinhDaoTao_ID);
            if (retvalue)
            {
                ChuongTrinhDaoTaoEntity oDeleted = FindChuongTrinhDaoTao(ChuongTrinhDaoTao_ID);
                if (oDeleted != null)
                {
                    _ListChuongTrinhDaoTao.Remove(oDeleted);
                }
            }
            return retvalue;
        }
        public void ResetObject()
        {
            this.oChuongTrinhDaoTao = new ChuongTrinhDaoTaoEntity();
        }
        //Check if exists MaCTDaoTao
        public bool CheckExistsMaCTDaoTao(object value)
        {
            return dao.SelectAllFilter("MaCTDaoTao='" + value.ToString() + "'").Length > 0;
        }
        //Copy data from data row in grid to object
        public ChuongTrinhDaoTaoEntity MapDataRowToObject(DataRow row)
        {
            ChuongTrinhDaoTaoEntity o = new ChuongTrinhDaoTaoEntity();
            o.MaCTDaoTao = Convert.ToString(row["MaCTDaoTao"]);
            o.TenCTDaoTao = Convert.ToString(row["TenCTDaoTao"]);
            if (!row.IsNull("NgayTao"))
                o.NgayTao = ConvertDateTime(row["NgayTao"]);
            o.GhiChu = Convert.ToString(row["GhiChu"]);
            if (!row.IsNull("NganhHoc_ID"))
                o.NganhHoc_ID = (Int32)(row["NganhHoc_ID"]);
            if (!row.IsNull("ChuongTrinhDaoTao_ID"))
                o.ChuongTrinhDaoTao_ID = (Int32)(row["ChuongTrinhDaoTao_ID"]);
            return o;
        }
        //Copy data from object to data row in grid    
        public void MapObjectToDataRow(ChuongTrinhDaoTaoEntity o, DataRow row)
        {
            row["MaCTDaoTao"] = o.MaCTDaoTao;
            row["TenCTDaoTao"] = o.TenCTDaoTao;
            row["NgayTao"] = o.NgayTao;
            row["GhiChu"] = o.GhiChu;
            row["NganhHoc_ID"] = o.NganhHoc_ID;
            row["ChuongTrinhDaoTao_ID"] = o.ChuongTrinhDaoTao_ID;
        }
        public void MapObjectToDataRow(DataRow row)
        {
            this.MapObjectToDataRow(this.ChuongTrinhDaoTaoObject, row);
        }
        public void LoadObject(Int32 ChuongTrinhDaoTao_ID)
        {
            ChuongTrinhDaoTaoEntity oLoad = FindChuongTrinhDaoTao(ChuongTrinhDaoTao_ID);
            if (oLoad == null)
            {
                oChuongTrinhDaoTao = this.LoadChuongTrinhDaoTao(ChuongTrinhDaoTao_ID);
                bool bNotIsNull = true;
                bNotIsNull &= oChuongTrinhDaoTao.IsNullChuongTrinhDaoTao_ID;
                if (!bNotIsNull)
                {
                    this.ListChuongTrinhDaoTao.Add(oChuongTrinhDaoTao);
                }
            }
            else
            {
                oChuongTrinhDaoTao = oLoad;
            }
        }
        #region CachedList ChuongTrinhDaoTao
        private static List<ChuongTrinhDaoTaoEntity> _ListChuongTrinhDaoTao = new List<ChuongTrinhDaoTaoEntity>();
        public List<ChuongTrinhDaoTaoEntity> ListChuongTrinhDaoTao
        {
            get
            {
                return _ListChuongTrinhDaoTao;
            }
        }
        public static List<ChuongTrinhDaoTaoEntity> ListChuongTrinhDaoTaoStatic
        {
            get
            {
                return _ListChuongTrinhDaoTao;
            }
        }
        public ChuongTrinhDaoTaoEntity FindChuongTrinhDaoTao(Int32 ChuongTrinhDaoTao_ID)
        {
            ChuongTrinhDaoTaoEntity oResult = this.ListChuongTrinhDaoTao.Find(delegate(ChuongTrinhDaoTaoEntity oFind)
            {
                bool result = true;
                result &= oFind.ChuongTrinhDaoTao_ID.Equals(ChuongTrinhDaoTao_ID);
                return result;
            });
            return oResult;
        }
        public static void LoadAllChuongTrinhDaoTao(bool clearCache)
        {
            if (_ListChuongTrinhDaoTao.Count == 0)
                clearCache = true;
            if (!clearCache) return;
            ChuongTrinhDaoTaoServiceClient dao = new ChuongTrinhDaoTaoServiceClient();
            _ListChuongTrinhDaoTao = new List<ChuongTrinhDaoTaoEntity>(dao.SelectAll());
            if (dao.State == System.ServiceModel.CommunicationState.Opened) try { dao.Close(); }
                catch { }
        }
        public ChuongTrinhDaoTaoEntity LoadChuongTrinhDaoTao(Int32 ChuongTrinhDaoTao_ID)
        {
            ChuongTrinhDaoTaoEntity o = dao.SelectOne(ChuongTrinhDaoTao_ID);
            return o;
        }
        #endregion
    }
}
